import { DELETE, GET, POST, PUT } from './index';

// ==================== Types ====================
export interface LoginRequest {
  username: string;
  password: string;
}

export interface UserInfo {
  id: number;
  username: string;
  nick_name?: string;
  email?: string;
  phone?: string;
  avatar_url?: string;
  status: number;
  remark?: string;
  roles?: RoleInfo[];
  gmt_created?: string;
  gmt_modified?: string;
}

export interface TokenInfo {
  access_token: string;
  token_type: string;
  expires_in: number;
}

export interface LoginResponse {
  user: UserInfo;
  token: TokenInfo;
  menus: MenuInfo[];
  permissions: string[];
}

export interface RoleInfo {
  id: number;
  role_code: string;
  role_name: string;
  role_desc?: string;
  status: number;
  remark?: string;
  menus?: MenuInfo[];
  gmt_created?: string;
  gmt_modified?: string;
}

export interface MenuInfo {
  id: number;
  parent_id: number;
  menu_code: string;
  menu_name: string;
  menu_name_en?: string;
  menu_type: number; // 1=Directory, 2=Menu, 3=Button
  route_path?: string;
  component_path?: string;
  icon?: string;
  sort_order: number;
  permission_code?: string;
  status: number;
  is_visible: number;
  remark?: string;
  children?: MenuInfo[];
  gmt_created?: string;
  gmt_modified?: string;
}

export interface UserRequest {
  id?: number;
  username: string;
  password?: string;
  nick_name?: string;
  email?: string;
  phone?: string;
  avatar_url?: string;
  status?: number;
  remark?: string;
  role_ids?: number[];
}

export interface RoleRequest {
  id?: number;
  role_code: string;
  role_name: string;
  role_desc?: string;
  status?: number;
  remark?: string;
  menu_ids?: number[];
}

export interface MenuRequest {
  id?: number;
  parent_id: number;
  menu_code: string;
  menu_name: string;
  menu_name_en?: string;
  menu_type: number;
  route_path?: string;
  component_path?: string;
  icon?: string;
  sort_order: number;
  permission_code?: string;
  status?: number;
  is_visible?: number;
  remark?: string;
}

export interface PaginationResult<T> {
  items: T[];
  total_count: number;
  total_pages: number;
  page: number;
  page_size: number;
}

// ==================== Auth APIs ====================

/**
 * 用户登录
 */
export const login = (data: LoginRequest) => {
  return POST<LoginRequest, LoginResponse>('/api/v1/serve/auth/auth/login', data);
};

/**
 * 用户登出
 */
export const logout = () => {
  return POST<null, boolean>('/api/v1/serve/auth/auth/logout');
};

/**
 * 获取当前用户信息
 */
export const getCurrentUser = () => {
  return GET<null, UserInfo>('/api/v1/serve/auth/auth/current_user');
};

// ==================== User APIs ====================

/**
 * 创建用户
 */
export const createUser = (data: UserRequest) => {
  return POST<UserRequest, UserInfo>('/api/v1/serve/auth/users', data);
};

/**
 * 更新用户
 */
export const updateUser = (id: number, data: UserRequest) => {
  return PUT<UserRequest, UserInfo>(`/api/v1/serve/auth/users/${id}`, data);
};

/**
 * 删除用户
 */
export const deleteUser = (id: number) => {
  return DELETE<null, boolean>(`/api/v1/serve/auth/users/${id}`);
};

/**
 * 获取用户详情
 */
export const getUser = (id: number) => {
  return GET<null, UserInfo>(`/api/v1/serve/auth/users/${id}`);
};

/**
 * 获取用户列表
 */
export const getUserList = (params: { page?: number; page_size?: number }) => {
  return GET<{ page?: number; page_size?: number }, PaginationResult<UserInfo>>(
    '/api/v1/serve/auth/users',
    params,
  );
};

// ==================== Role APIs ====================

/**
 * 创建角色
 */
export const createRole = (data: RoleRequest) => {
  return POST<RoleRequest, RoleInfo>('/api/v1/serve/auth/roles', data);
};

/**
 * 更新角色
 */
export const updateRole = (id: number, data: RoleRequest) => {
  return PUT<RoleRequest, RoleInfo>(`/api/v1/serve/auth/roles/${id}`, data);
};

/**
 * 删除角色
 */
export const deleteRole = (id: number) => {
  return DELETE<null, boolean>(`/api/v1/serve/auth/roles/${id}`);
};

/**
 * 获取角色详情
 */
export const getRole = (id: number) => {
  return GET<null, RoleInfo>(`/api/v1/serve/auth/roles/${id}`);
};

/**
 * 获取角色列表
 */
export const getRoleList = (params: { page?: number; page_size?: number }) => {
  return GET<{ page?: number; page_size?: number }, PaginationResult<RoleInfo>>(
    '/api/v1/serve/auth/roles',
    params,
  );
};

// ==================== Menu APIs ====================

/**
 * 创建菜单
 */
export const createMenu = (data: MenuRequest) => {
  return POST<MenuRequest, MenuInfo>('/api/v1/serve/auth/menus', data);
};

/**
 * 更新菜单
 */
export const updateMenu = (id: number, data: MenuRequest) => {
  return PUT<MenuRequest, MenuInfo>(`/api/v1/serve/auth/menus/${id}`, data);
};

/**
 * 删除菜单
 */
export const deleteMenu = (id: number) => {
  return DELETE<null, boolean>(`/api/v1/serve/auth/menus/${id}`);
};

/**
 * 获取菜单详情
 */
export const getMenu = (id: number) => {
  return GET<null, MenuInfo>(`/api/v1/serve/auth/menus/${id}`);
};

/**
 * 获取菜单树
 */
export const getMenuTree = () => {
  return GET<null, { menus: MenuInfo[] }>('/api/v1/serve/auth/menus/tree');
};

/**
 * 获取菜单列表
 */
export const getMenuList = (params: { page?: number; page_size?: number }) => {
  return GET<{ page?: number; page_size?: number }, PaginationResult<MenuInfo>>(
    '/api/v1/serve/auth/menus',
    params,
  );
};
